package com.burgeon.backend.Rest.membership;
/**
 * 获取会员升降级记录
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.entity.Membership;
import com.google.gson.JsonArray;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class GetMembershipData extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public GetMembershipData(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     */

    public ArrayList<Membership> getData() {
        String url = "http://" + ip + "/servlets/binserv/Rest";


        try {
            String str = RESULT(Data(), url);

            System.err.println(str);
            String code = new JSONArray(str).getJSONObject(0).getString("code");
            if (!"0".equals(code)) {
                throw new Exception();
            }

            JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
            ArrayList<Membership> data = new ArrayList<Membership>();

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONArray arr = jsonArray.getJSONArray(i);
                Membership membership = new Membership();
                membership.setId(arr.getString(0));
                membership.setTime(arr.getString(1));
                membership.setCode(arr.getString(2));
                membership.setOldType(arr.getString(3));
                membership.setNewType(arr.getString(4));
                membership.setOpenid(arr.getString(5));
                data.add(membership);
            }
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    private MultiValueMap Data() {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");
        columns.add("BILLDATE");
        columns.add("'C_VIP_ID;CARDNO'");
        columns.add("'C_VIPTYPE_ID;NAME'");//原等级
        columns.add("'NEW_C_VIPTYPE_ID;NAME'");//现等级
        columns.add("'C_VIP_ID;OPENID'");

        params.put("columns", columns);
        params.put("table", "VIP_LV_UP");//表名

      /*  HashMap paramss = new HashMap();

        paramss.put("column", "IS_PUSH");
        paramss.put("condition", 'N');
        params.put("params", paramss);*/



        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        expr1.put("column","IS_PUSH");
        expr1.put("condition","N");
        HashMap expr2 = new HashMap();
        expr2.put("column","'C_VIP_ID;OPENID'");
        expr2.put("condition","is not null");

        paramss.put("combine","and");
        paramss.put("expr1",expr1);
        paramss.put("expr2",expr2);

        params.put("params",paramss);


        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        GetMembershipData g = new GetMembershipData("8.130.161.41:55321", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
